Page({
data: {
  accountname: '',
  account: '',
  date:'',
  lastremain:'',
  tableaccount:'',
  remainmoney:''
},// 标题文本框

bindTitleInput: function(e) {
  this.setData({
    accountname: e.detail.value
  }) 
},// 名称

bindAccountInput: function(e) {
  this.setData({
    account: e.detail.value
  }) 
  //console.log(this.data.date[5] + this.data.date[6])
},// 金额文本框

bindDateChange: function (e) {
  this.setData({
    date: e.detail.value
  })
  
},//日期

save: function(){

  if (this.data.accountname&&this.data.account){
    var patrn = /^(-)?\d+(\.\d+)?$/;
    if (patrn.exec(this.data.account) == null || this.data.account == ""){
      wx.showModal({
        title: '请数字正确数字',
      })
      this.setData({
        cost: '',
      })
    }
    else{
    let tableID=37774
    let Product = new wx.BaaS.TableObject(tableID)
    let product = Product.create()
    
    Product.find().then(res => {
      this.setData({
        lastremain : res.data.objects[res.data.objects.length - 1].remain
      })
      this.data.remainmoney =String(parseFloat(this.data.lastremain) + parseFloat(-this.data.account))
      let Record = {
        title: this.data.accountname,
        account: '-' + this.data.account,
        date: this.data.date,
        remain: this.data.remainmoney
      }
      product.set(Record).save()
    }, err => {
      wx.showModal({
        title: '当前数据库为空！',
      })
    })

  wx.showModal({
    title: '插入成功！',
    content: '',
  })
  this.setData({
    title:'',
    cost: '',
  })
    }
  }else{
    wx.showModal({
      title: '标题或金额为空，请重新输入',
    })
  }
}, // 使用onShow而不使用onLoad，使得添加返回后自刷新



onLoad: function (options) {
  // 初始化日期
  //    获取当前日期
  var date = new Date();
  //    格式化日期为"YYYY-mm-dd"
  var dateStr = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();
  //    存回date，以渲染到页面
  
  this.setData({
    date: dateStr
  })
},

hideAlertView: function() {
  this.setData({
    'modalHidden': true

  }) ,// 返回上一页
  
  wx.navigateBack()

}

})
